package com.server.modules.api.service.impl;

import com.lowagie.text.pdf.codec.Base64;
import com.server.common.service.CrudService;
import com.server.common.utils.MD5Utils;
import com.server.common.utils.PathUtils;
import com.server.common.utils.UUIDUtils;
import com.server.modules.api.service.IFileService;
import com.server.modules.api.vo.FileVO;
import com.server.modules.sys.dao.material.MaterialUploadDao;
import com.server.modules.sys.entity.material.MaterialUpload;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Calendar;

/**
 * Created by Administrator on 2016/8/22.
 */
@Service
public class FileServiceImpl  extends CrudService<MaterialUploadDao, MaterialUpload> implements IFileService {
    @Override
    @Transactional(readOnly = false)
    public String uploadFile(FileVO vo) {
        MaterialUpload materialUpload = new MaterialUpload();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        String dateDir =  year + File.separator + month
                + File.separator + day + File.separator;
        File tmpDir = new File(PathUtils.getSaveImagePath()+dateDir);
        if (!tmpDir.exists()) {
            tmpDir.mkdirs();
        }
        // 构造新的图片名称
        String uuid = UUIDUtils.getUUID();
        String fileName = uuid + "."+vo.getExt();
        materialUpload.setUrl(dateDir+fileName); //文件URL
        String savePath = PathUtils.getSaveImagePath()+ dateDir + fileName;
        if(Base64.decodeToFile(vo.getImageHash().trim(),savePath )){
            try {
                materialUpload.setMd5(MD5Utils.md5Encode(vo.getImageHash().trim()));
            }catch (Exception e){
                logger.error("MD5异常");
                return null;
            }
            materialUpload.setGroupId(UUIDUtils.getUUID());
            materialUpload.setMemberId(vo.getMemberId());
            materialUpload.setTypeId(vo.getTypeId());
            materialUpload.setCreateBy(vo.getUserBy());
            materialUpload.setOrderBy(vo.getOrder());
            super.save(materialUpload);
            return materialUpload.getUrl();
        }else{
            return null;
        }
    }

    @Override
    public String saveFile(FileVO vo){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        String dateDir =  year + File.separator + month + File.separator + day + File.separator;
        File tmpDir = new File(PathUtils.getSaveImagePath()+dateDir);
        if (!tmpDir.exists()) {
            tmpDir.mkdirs();
        }
        // 构造新的图片名称
        String uuid = UUIDUtils.getUUID();
        String fileName = uuid + "."+vo.getExt();
        String savePath = PathUtils.getSaveImagePath()+ dateDir + fileName;
        if(Base64.decodeToFile(vo.getImageHash().trim(),savePath )){
            return PathUtils.getImageURL()+ dateDir + fileName;
        }else return null;
    }
}
